package org.lgl.goods.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.lgl.common.utils.IdWorker;
import org.lgl.goods.dao.BrandDao;
import org.lgl.goods.dao.CategoryDao;
import org.lgl.goods.dao.GoodsDao;
import org.lgl.goods.dao.SpecsDao;
import org.lgl.goods.model.Brand;
import org.lgl.goods.model.Category;
import org.lgl.goods.model.Goods;
import org.lgl.goods.model.Specs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class GoodsService {

    private final GoodsDao goodsDao;

    private final CategoryDao categoryDao;

    private final BrandDao brandDao;

    private final SpecsDao specsDao;

    private final IdWorker idWorker;

    @Autowired
    public GoodsService(GoodsDao goodsDao, CategoryDao categoryDao, BrandDao brandDao, SpecsDao specsDao, IdWorker idWorker) {
        this.goodsDao = goodsDao;
        this.categoryDao = categoryDao;
        this.brandDao = brandDao;
        this.specsDao = specsDao;
        this.idWorker = idWorker;
    }

    public List<Goods> genMockGoodsByName(String baseName, Long categoryId, Long brandId, Long stock, Long minPrice, Long maxPrice) {
        Category category = categoryDao.selectByPrimaryKey(categoryId);
        Brand brand = brandDao.selectByPrimaryKey(brandId);
        // 临时就用一张图片， 在真正的项目里会用到云存储工具，比如 阿里云的OSS等，直接看文档调用api就可以了
        String img = "/img/HuaweiMateX2.jpeg";
        Example example = new Example(Specs.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("templateId", category.getTemplateId());
        // 获取类目对应模板的规格列表
        List<Specs> specsList = specsDao.selectByExample(example);
        List<String> fullNames = genGoodsFullName(0, specsList);
        List<Goods> res = new ArrayList<>();
        for (String fullName : fullNames) {
            Goods goods = Goods.builder()
                    // 使用雪花算法生成分库分表唯一id
                    .id(idWorker.nextId())
                    .name(baseName + " " + fullName)
                    .price(minPrice + (long) (Math.random() * (maxPrice - minPrice)))
                    .stock(stock)
                    .image(img)
                    .brandId(brandId)
                    .brandName(brand.getName())
                    .categoryId(categoryId)
                    .categoryName(category.getName())
                    .paramJson("")
                    .build();
            JSONObject specsJson = new JSONObject();
            String[] fullNameSplits = fullName.split(";");
            for (int i = 0; i < specsList.size(); i++) {
                specsJson.put(specsList.get(i).getName(), fullNameSplits[i]);
            }
            goods.setSpecsJson(specsJson.toJSONString());
            res.add(goods);
            goodsDao.insertSelective(goods);
        }
        return res;
    }

    /**
     * 递归生成 商品
     *
     * @param startLineNum
     * @param specsList
     * @return
     */
    private List<String> genGoodsFullName(int startLineNum, List<Specs> specsList) {
        List<String> res = new ArrayList<>();
        if (startLineNum == specsList.size() - 1) {
            res.addAll(Arrays.asList(specsList.get(startLineNum).getOptions().split(",")));
            return res;
        }
        String[] specOptions = specsList.get(startLineNum).getOptions().split(",");
        List<String> subRes = genGoodsFullName(startLineNum + 1, specsList);
        for (String option : specOptions) {
            for (String subName : subRes) {
                res.add(option + ";" + subName);
            }
        }
        return res;
    }

    public List<Goods> getGoodsByStatus(int status) {
        Goods goods = new Goods();
        goods.setStatus(status);
        // 查找 状态正常的 商品列表
        return goodsDao.select(goods);
    }

    public Goods getGoodsById(long id) {
        return goodsDao.selectByPrimaryKey(id);
    }

}
